package com.mycompany.j2eem.controller;

import com.mycompany.j2eem.Entity.JsonResult;
import com.mycompany.j2eem.Entity.ResultCode;
import com.mycompany.j2eem.Entity.bean.PicInfo;
import com.mycompany.j2eem.Entity.bean.PictureInDBBean;
import com.mycompany.j2eem.Entity.bean.PictureObjectBean;
import com.mycompany.j2eem.service.common.Download;
import com.mycompany.j2eem.service.ghyservice.PictureStoreService;
import com.mycompany.j2eem.service.wrjservice.RecommendationService;
import com.mycompany.j2eem.service.wrjservice.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class PictureController {

    @Autowired
    RecommendationService recommendationService;
    @Autowired
    SearchService searchService;
    @Autowired
    PictureStoreService pictureStoreService;

    //热门图片（10-20张一次）
    @RequestMapping("/HotPicture")//ok
    public JsonResult hotPicture() {

        long a = System.currentTimeMillis();
        List<PictureInDBBean> avail = new ArrayList<>();
        Download download = new Download();

        while (avail.size() < 15) {
            List<String> hotTag = recommendationService.getTagIdService();

            List<PictureInDBBean> hotPictureList = recommendationService.getPic(hotTag);

            List<PictureInDBBean> loop = searchService.hotFilter(hotPictureList);
//            if (hotPictureList.size()==0) {
//                return new JsonResult(ResultCode.NOT_DATA);
//            }
            for (PictureInDBBean s : loop) {
                //wucy改
                if (download.downloadPicture(s.getPicturepath())) {
                    avail.add(s);
                }
            }
        }

        System.out.println("推荐时长" + (System.currentTimeMillis() - a));
        return new JsonResult(ResultCode.SUCCESS, "成功", avail);
    }

    //个人推荐图片（10-20张一次）
    @RequestMapping("/RecommendPicture")
    public JsonResult recommendPicture(HttpSession session) {

        Download download = new Download();
        Object userName = session.getAttribute("username");

        //  Object userName=new String("1");
        List<PictureInDBBean> avail = new ArrayList<>();
        if (userName == null) {
            return hotPicture();
        } else {

            if (avail.size() == 0) {
                // return new JsonResult(ResultCode.NOT_DATA);
                return hotPicture();
            } else if (avail.size() < 15) {
                List<PictureObjectBean> recommendPictureList = recommendationService.recommend_pic_service("15161607893", false);
                List<PictureInDBBean> loop = searchService.personalRecFilter(recommendPictureList);
                for (PictureInDBBean s : loop) {
                    //wucy改
                    if (download.downloadPicture(s.getPicturepath())) {
                        avail.add(s);
                    }
                }
            }
            return new JsonResult(ResultCode.SUCCESS, "成功", avail);
        }
    }

    //标签搜索结果(10-20张图片一次)
    @RequestMapping("/SearchResult")//ok
    public JsonResult searchResult(@RequestParam("tags") String tags, HttpSession session) {

        long start = System.currentTimeMillis();
        String tag = tags;
//        try {
//            tag = new String(tags.getBytes("ISO-8859-1"), "UTF-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        if (tag == null) {
            return recommendPicture(session);
        }

        //数据库查询结果
        //返回图片url id
        List<String> availTags = searchService.searchForTag(tag);
        List<String> pointedTag = searchService.searchForPointedTag(tag);
        if (pointedTag == null) {
            return new JsonResult(ResultCode.NOT_DATA);
        }
        List<List<PictureInDBBean>> pictureInDBS = searchService.searchForPics(pointedTag);
        List<PictureInDBBean> avail = searchService.TagFilter(pictureInDBS);
        long end = System.currentTimeMillis() - start;
        System.out.println(tag + "耗时间： " + end);
        return new JsonResult(ResultCode.SUCCESS, "成功", avail);

    }

    //查看图片具体信息
    @RequestMapping("/PictureInfo")
    public JsonResult pictureInfo(@RequestParam("pictureID") String pictureID, HttpSession session) {
        //TODO
        //返回图片的具体信息
        //检查权限是否足够（是否可以查看上传但未发布的图片）
        //username nickname createTime starNumber isStar

        Map<String, Object> pictureInfo = null;
        if (pictureInfo == null) {
            return new JsonResult(ResultCode.NOT_DATA);
        }
        return new JsonResult(ResultCode.SUCCESS, "成功", pictureInfo);
    }

    //收藏图片
    @RequestMapping("/Star")
    public JsonResult star(@RequestParam("pictureID") String pictureID, HttpSession session) {
        //TODO
        //将数据存入数据库，并用于推荐
        //获取用户ID
        String username = session.getAttribute("username").toString();

        JsonResult result = pictureStoreService.ConcernPicture(username, pictureID);

        if (result.getMessage().equals("成功")) {
            return new JsonResult(ResultCode.SUCCESS);
        }
        return new JsonResult(ResultCode.FAIL, "收藏失败");
    }

    //取消收藏
    @RequestMapping("/CancelStar")
    public JsonResult cancelStar(@RequestParam("pictureID") String pictureID, HttpSession session) {
        //TODO
        //修改数据库，并用于推荐
        String username = session.getAttribute("username").toString();
        JsonResult result = pictureStoreService.deConcernPicture(username, pictureID);
        // pictureStoreService.deConcernPicture(username,pictureID);
        if (result.getMessage().equals("成功")) {
            return new JsonResult(ResultCode.SUCCESS);
        }
        return new JsonResult(ResultCode.FAIL, "取消失败");
    }

    //个人界面发布的图片信息（1-15张）
    @RequestMapping("/PersonalPictureInfo")
    public JsonResult personalPictureInfo(@RequestParam("username") String username, HttpSession session) {
        //TODO
        //如果username与session中的username相同 返回个人上传的图片
        //若不同 返回发布的图片
        //返回图片 url creatTime
        List<Map<String, Object>> pictureInfo = pictureStoreService.personalMovementInfo(username);
        if (pictureInfo == null) {
            return new JsonResult(ResultCode.NOT_DATA);
        }
        return new JsonResult(ResultCode.SUCCESS, "成功", pictureInfo);

    }

    //单独图片的信息
    @RequestMapping("/SinglePictureInfo")
    public JsonResult singlePictureInfo(@RequestParam("pictureID") String pictureID, HttpSession session) {
       // String username = session.getAttribute("username").toString();
        PicInfo pictureInfo = pictureStoreService.pictureInfo(pictureID);
        if (pictureInfo == null) {
            return new JsonResult(ResultCode.NOT_DATA);
        }else
        return new JsonResult(ResultCode.SUCCESS, "成功", pictureInfo);

    }

    //单独图片的路径
    @RequestMapping("/PicturePath")
    public JsonResult picturePath(@RequestParam("pictureID") String pictureID, HttpSession session) {
        String username = session.getAttribute("username").toString();
        PicInfo pictureInfo = pictureStoreService.pictureInfo(pictureID);
        if (pictureInfo == null) {
            return new JsonResult(ResultCode.NOT_DATA);
        }
        return new JsonResult(ResultCode.SUCCESS, "成功", pictureInfo);
    }
}
